define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
          Table.api.init({
            extend: {
              index_url: 'integral/bookmanager/index/ids/'+week_ids,
              add_url: 'integral/bookmanager/add/ids/'+week_ids,
              edit_url: 'integral/bookmanager/edit/ids/'+week_ids,
              del_url: 'integral/bookmanager/del',
              multi_url: 'integral/bookmanager/multi',
              import_url: 'integral/bookmanager/import/ids/'+week_ids,
              bookmanageredit_url:'integral/bookmanager/bookmanageredit/ids/'+week_ids,
              table: 'book_manager'
            }
          });

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
            $('.btn-bookmanageredit').on('click', function () {
              var that = this;
              Fast.api.open($.fn.bootstrapTable.defaults.extend.bookmanageredit_url, __('编辑关注人数/拍照数'), $(that).data() || {});
            });
            
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    columns: [
                      [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'week_id_text', title: __('Week_id')},
                        {field: 'userid_text', title: __('User_id')},
                        // {field: 'number', title: __('通联方式数量')},
                        {field: 'integral', title: __('积分')},
                        {
                          field: 'browser',
                          title: __('通联方式数量,详情'),
                          operate: false,
                          events: Table.api.events.browser,
                          formatter: Controller.api.formatter.browser
                        }
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                      ]
                    ],
                    //禁用默认搜索
                    search: false,
                    //启用普通表单搜索
                    commonSearch: false,
                    //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                    searchFormVisible: false,
                    //分页大小
                    pageList: 'All',
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },

            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'integral/fixednumber/index',
                    extend: {
                      index_url: 'integral/fixednumber/index',
                      add_url: 'integral/fixednumber/add/ids/'+week_ids,
                      edit_url: 'integral/fixednumber/edit',
                      del_url: 'integral/fixednumber/del',
                      multi_url: 'integral/fixednumber/multi',
                      table: 'fixed_number',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    columns: [
                        [
                          {checkbox: true},
                          {field: 'id', title: __('Id'),operate: false},
                          // {field: 'week_id', title: __('Week_id')},
                          {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                          {field: 'number', title: __('Number'),operate: false},
                          {field: 'userid', title: __('Userid'),operate: false},
                          {
                            field: 'effective',
                            title: __('是否有效'),
                            editable: {
                              type: 'select',
                              pk: 1,
                              source: [
                                {value: '1', text: ' 有效 '},
                                {value: '2', text: ' 无效 '},
                              ]
                            },
                           searchList: {"1":__('有效'),"2":__('无效')},
                          },
                          {
                            field: 'browser',
                            title: __('积分'),
                            operate: false,
                            events: Table.api.events.browser,
                            formatter: Controller.api.formatter.second,
                          },
                          {field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ],
                    queryParams: function (params) {
                      //这里可以追加搜索条件
                      var filter = JSON.parse(params.filter);
                      var op = JSON.parse(params.op);
                      //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                      filter.week_id = week_ids;
                      op.week_id = "=";
                      params.filter = JSON.stringify(filter);
                      params.op = JSON.stringify(op);
                      return params;
                    },
                    //禁用默认搜索
                    search: false,
                    //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                    searchFormVisible: false,
                    //分页大小
                    pageList: 'All',
                });
                // 为表格2绑定事件
                Table.api.bindevent(table2);
            },

            tree: function () {
              // 表格3
              var table3 = $("#table3");
              table3.bootstrapTable({
                url: 'integral/bookstore/index',
                extend: {
                  index_url: 'integral/bookstore/index',
                  add_url: 'integral/bookstore/add/ids/'+week_ids,
                  edit_url: 'integral/bookstore/edit',
                  del_url: 'integral/bookstore/del',
                  multi_url: 'integral/bookstore/multi',
                  import_url: 'integral/bookstore/import',
                  table: 'integral_bookstore',
                },
                toolbar: '#toolbar3',
                sortName: 'id',
                search: false,
                columns: [
                  [
                    {checkbox: true},
                    {field: 'id', title: 'ID'},
                    {field: 'bookstore_name', title: __('Bookstore_name'),editable:true},
                    {field: 'boss', title: __('Boss'),editable:true},
                    {field: 'tel', title: __('Tel')},
                    {field: 'control', title: __('Control')},
                    {field: 'discount', title: __('Discount'), operate:'BETWEEN'},
                    {field: 'admin.username', title: __('Admin.username')},
                    {
                      field: 'status',
                      title: __('Status'),
                      editable: {
                        type: 'select',
                        pk: 1,
                        source: [
                          {value: '1', text: ' 铺货 '},
                          {value: '2', text: ' 现款 '},
                        ]
                      }
                    },
                    {field: 'operate', title: __('Operate'), table: table3, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                  ]
                ],
                queryParams: function (params) {
                  //这里可以追加搜索条件
                  var filter = JSON.parse(params.filter);
                  var op = JSON.parse(params.op);
                  //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                  filter.week_id = week_ids;
                  op.week_id = "=";
                  params.filter = JSON.stringify(filter);
                  params.op = JSON.stringify(op);
                  return params;
                },
              });
              // 为表格3绑定事件
              Table.api.bindevent(table3);
              // 为表格绑定事件
              var submitForm = function (ids, layero) {
                var options = table3.bootstrapTable('getOptions');
                console.log(options);
                var columns = [];
                $.each(options.columns[0], function (i, j) {
                  if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                    columns.push(j.field);
                  }
                });
                var search = options.queryParams({});
                $("input[name=search]", layero).val(options.searchText);
                $("input[name=ids]", layero).val(ids);
                $("input[name=filter]", layero).val(search.filter);
                $("input[name=op]", layero).val(search.op);
                $("input[name=columns]", layero).val(columns.join(','));
                $("form", layero).submit();
              };
              $(document).on("click", ".btn-export", function () {
                var ids = Table.api.selectedids(table3);
                var page = table3.bootstrapTable('getData');
                var all = table3.bootstrapTable('getOptions').totalRows;
                console.log(ids, page, all);
                Layer.confirm("请选择导出的选项<form action='" + Fast.api.fixurl("integral/bookstore/export") + "' method='post' target='_blank'><input type='hidden' name='ids' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'><input type='hidden' name='search'><input type='hidden' name='columns'></form>", {
                  title: '导出数据',
                  btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                  success: function (layero, index) {
                    $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                  }
                  , yes: function (index, layero) {
                    submitForm(ids.join(","), layero);
                    return false;
                  }
                  ,
                  btn2: function (index, layero) {
                    var ids = [];
                    $.each(page, function (i, j) {
                      ids.push(j.id);
                    });
                    submitForm(ids.join(","), layero);
                    return false;
                  }
                  ,
                  btn3: function (index, layero) {
                    submitForm("all", layero);
                    return false;
                  }
                })
              });
          }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
          formatter: {//渲染的方法
            browser: function (value, row, index) {
              return row.number+'&nbsp;&nbsp;&nbsp;<a href="/admin/integral/teachertel/index/ids/'+row.week_id+'/userid/'+row.userid+'" class="btn btn-xs btn-primary btn-dialog" title="老师通联方式详细" data-table-id="table" data-field-index="11" data-row-index="0" data-button-index="0"><i class="fa fa-list"></i>老师通联方式</a>';
            },
            second: function (value, row, index) {
              if(row.effective == 1 && row.type == 1)
              {
                return row.number * 6;
              }
              if(row.effective == 1)
              {
                return row.number * 3;
              }
              return 0;
            }
          }
        }
    };
    return Controller;
});